checkLogin(true);
$(function () {
    var pageCount = 10;
    var buttonCount = 5;
    var dataCount, maxCount, start, end;
    var current = 1;
    var data = {};
    var article_id;

    function getArticlesByCondition(data) {
        data = data || {};
        data.currentPage = current;
        data.pageCount = pageCount;
        $.post("../php/articles/getArticlesByPageByCondition.php", data, function (res) {
            if (res.code == 100) {
                var html = $("#mt").tmpl(res.data);
                $("#dataset").empty().append(html);
            }
            else {
                layer.alert(res.msg, {icon: 2});
            }
            dataCount = res.dataCount;
            maxCount = Math.ceil(dataCount / pageCount);
            start = current - Math.floor(buttonCount / 2);
            if (start < 1) {
                start = 1;
            }
            end = start + (buttonCount - 1);
            if (end > maxCount) {
                end = maxCount;
                start = end - (buttonCount - 1);
            }
            var html = "";
            if (current != 1) {
                html += "<li><a href='#' page='" + (current - 1) + "' class='page-button'>上一页</a></li>"
            }
            for (var i = start; i <= end; i++) {
                html += "<li><a href='#' page='" + i + "' class='page-button'>" + i + "</a></li>"
            }
            if (current != maxCount) {
                html += "<li><a href='#' page='" + (current + 1) + "' class='page-button'>下一页</a></li>"
            }
            $("#pagination").html(html);
        }, "json");
    }

    $("#pagination").on("click", ".page-button", function () {
        var thisPage = parseInt($(this).attr("page"));
        current = thisPage;
        getArticlesByCondition(data);
    })

    $.post("../php/categories/getCategoriesAll.php", function (res) {
        if (res.code == 100) {
            var html = "<option value=''>所有分类</option>";
            for (var i = 0; i < res.data.length; i++) {
                html += "<option value='" + res.data[i].id + "'>" + res.data[i].name + "</option>";
            }
            $("#categories").html(html)
            data = common();
            getArticlesByCondition(data);
        }
    }, "json");

    $("#queryby").on("click", function () {
        current = 1;
        data = common();
        getArticlesByCondition(data);
    })

    function common() {
        var categoriy_id = $("#categories").val();
        var status = $("#status").val();
        if (categoriy_id != "") {
            data.category_id = categoriy_id;
        }
        if (status != "") {
            data.status = status;
        }
        return data;
    }

    $("#dataset").on("click", ".edit", function () {
        article_id = $(this).parent().parent().attr("article_id");
        location.href = "addArticle.html?id=" + article_id;
    })

    $("#dataset").on("click", ".del", function () {
        article_id = $(this).parent().parent().attr("article_id");
        layer.confirm("您是否要删除该文章", {icon: 3}, function (index) {
            if (index) {
                $.post("../php/articles/deleteArticle.php", {id: article_id}, function (res) {
                    if (res.code == 100) {
                        layer.alert(res.msg, {icon: 1}, function () {
                            location.reload();
                        });
                    }
                    else {
                        layer.alert(res.msg, {icon: 2}, function () {
                            location.reload();
                        });
                    }
                }, "json");
            }
        })
    })

    $("thead input[type=checkbox]").on("click", function () {
        var res = $(this).prop("checked");
        $("#dataset input[type=checkbox]").prop("checked", res);
        if (res) {
            $("#del-mul").show();
        } else {
            $("#del-mul").hide();
        }
    })

    $("#dataset").on("click", "input[type=checkbox]", function () {
        var count = $("#dataset input[type=checkbox]:checked").size();
        $("thead input[type=checkbox]").prop("checked", count == pageCount);
        if (count >= 2) {
            $("#del-mul").show();
        } else {
            $("#del-mul").hide();
        }
    });

    $("#del-mul").on("click", function () {
        layer.confirm("您是否要删除所选取的数据", {icon: 3}, function (index) {
            layer.close(index);
            var ids = [];
            var cks = $("#dataset input[type=checkbox]:checked");
            cks.each(function (index, element) {
                var id = $(element).parent().parent().attr("article_id");
                ids.push(id);
            })
            $.post("../php/articles/deleteArticlesMultipleById.php", {ids: ids}, function (res) {
                if (res.code == 100) {
                    layer.alert(res.msg, {icon: 1, title: "您正在进行批量删除操作"}, function () {
                        location.reload();
                    })
                }
                else {
                    layer.alert(res.msg, {icon: 2, title: "您正在进行批量删除操作"}, function () {
                        location.reload();
                    })
                }
            }, "json");
        })
    })
});